window.onload = function(){
    var index = parent.layer.getFrameIndex(window.name);
   
    $("#name").val(groupName);
    //alert(groupName)
    var mg=myHosts(groupName)
    $.ajax({
            type:"POST",
            url:constUrl+"HostServlet",
            dataType:"json",
            data: {
                operateType : "getAllHostName",
            },
            success:function(data){
                var temp = "";
                for (var i=0; i<data.hostName.length; i++) {
                    temp += '<option value="' + data.hostName[i] + '">' + data.hostName[i] + '</option>';
                	}
               $("#mySelect2").html(temp);
               $("#mySelect2").select2();
            	$("#mySelect2").val(mg)
            	$('#mySelect2').trigger('change.select2');
            },
            error:function (e) {
                console.log(e);
            }
        });
        /*$('#mySelect2').select2({
            placeholder: '请选择计算节点！'
        });*/
    $('#updateCategory').click(function () {
        var name = $("#hostGroupName").val();
        var introduction = $("#introduction").val();
        var validators = new  InputValidators();

        validators.importStrategies(validationStrategies);

        validators.addValidationStrategy('isEqual',  function(element, errMsg, value1, value2) {
            if(value1 !== value2) {
                return this.buildInvalidObj(element, errMsg, value1 );
            }
        });
        var category_name = document.getElementById('name');

        validators.addValidator('isNoEmpty', category_name, '分组名称未填写，请填写完整', category_name.value);
        //var vategory_classification = document.getElementById('myselect2');
        //validators.addValidator('isNoEmpty', vategory_classification, '分类类型未选择，请返回选择', vategory_classification.value);
        var result = validators.check();
        if(result){
            layer.msg(result.errMsg);
            // alert(result.errMsg);
            result.element.focus();
            return false;
        }
        updateGroup(groupName);
    });
    $('#closeIframe').click(function(){
        parent.layer.close(index);
    });
}
//验证准备
//输入验证器
function InputValidators(){
    this.validators = [];
    this.strategies = {};
}
//添加验证方法
//参数:
//  rule: 验证策略字符串
//  element: 被验证的dom元素
//  errMsg: 验证失败时显示的提示信息
//  value: 被验证的值
InputValidators.prototype.addValidator = function(rule, element, errMsg, value) {
    var that = this;
    var ruleElements = rule.split(":");

    this.validators.push(function() {
        var strategy = ruleElements.shift();
        var params = ruleElements;
        params.unshift(value);
        params.unshift(errMsg);
        params.unshift(element);
        return that.strategies[strategy].apply(that, params);
    });
};

//添加验证策略函数
//参数:
//  name: 策略名称
//  strategy: 策略函数
InputValidators.prototype.addValidationStrategy = function(name, strategy){
    this.strategies[name] = strategy;
};

//从策略对象导入验证策略函数
//参数:
//  strategies: 包含各种策略函数的对象
InputValidators.prototype.importStrategies = function(strategies) {
    for(var strategyName in strategies) {
        this.addValidationStrategy(strategyName, strategies[strategyName]);
    }
};

//验证失败时，将相关的错误信息打包返回
//参数:
//  element: dom元素
//   errMsg: 验证失败时的提示消息
//    value: 被验证的值
InputValidators.prototype.buildInvalidObj = function(element, errMsg, value){
    return {
        'value': value,
        'element': element,
        'errMsg': errMsg
    };
};

//开始验证
InputValidators.prototype.check = function() {
    for(var i = 0, validator; validator = this.validators[i++];){
        var result = validator();
        if(result) {
            return result;
        }
    }
};

var validationStrategies = {
    isNoEmpty: function(element, errMsg, value) {
        if(value === '') {
            return this.buildInvalidObj(element, errMsg, value );
        }
    }
};

function closeIframe(){
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}
function updateGroup(groupName){
	console.info($('#updateHostGroup').serializeArray())
	//console.info($('#hostAndGroup').serializeArray().concat($('#gpuInfo').serializeArray()));
	/*layer.confirm('确定要下线吗？', {
        btn: ['确定','取消'] //按钮
    },*///function(){
	var index = parent.layer.getFrameIndex(window.name);
		 $.ajax({
	        method : 'PUT',
	        url : constUrl+'conf/hosts/group/'+groupName+'.action',
	        dateType:'json',
	        data : $('#updateHostGroup').serializeArray(),
	        success : function(data) {
	        	if(data.resultCode=='000000'){
	        		layer.confirm('操作成功！',{
	                    btn:['确定']
	                },function () {
	                    parent.location.reload(index);
		            });
	         }else
	           layer.msg(data.desc)	        
	          },
	        error:function(){
	        	layer.msg('服务器内部错误，操作失败')
	          }
		 });
	//}
		 /*,function(){
		 layer.msg('取消删除', {
	            time: 1000 //1s后自动关闭
	        });
	});*/
	return false;
}
function myHosts(groupName){
	var res;
	$.ajax({
        method : 'get',
        url : constUrl+'conf/hosts/group/'+groupName+'.action',
        dateType:'json',
        async:false,
        success : function(data) {
        	if(data.resultCode=='000000'){
        		res=data.data;
        	}       
          },
        error:function(){
          }
	 });
	return res;
}
